package com.zhaorui.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.BufferUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhaorui.aop.Annotation2;
import com.zhaorui.entity.ActivityDictionary;
import com.zhaorui.entity.ActivitySysLog;
import com.zhaorui.entity.ActivityUser;
import com.zhaorui.entity.ZR;
import com.zhaorui.form.DictionaryAddFrom;
import com.zhaorui.form.DictionaryFrom;
import com.zhaorui.form.UserAddForm;
import com.zhaorui.service.ActivityDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/dictionary")
@Slf4j
@CrossOrigin
@Api(tags = "字典管理接口")
public class ActivityDictionaryController {
    @Autowired
    private ActivityDictionaryService activityDictionaryService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 等级下拉框查询：
     *
     * @return
     */
    @GetMapping("/queryLevel")
    @ApiOperation("等级查询")
    public ZR queryLevel() {
        return new ZR (1000, "查询成功", activityDictionaryService.queryLevel ());
    }

    /**
     * 等级综合查询：
     *
     * @return
     */
    @GetMapping("/queryAllLevel")
    @ApiOperation("等级综合查询")
    public ZR queryAllLevel(DictionaryAddFrom dictionaryAddFrom) {
        ActivityDictionary activityDictionary = new ActivityDictionary ();
        //赋值到activityDictionary
        BeanUtil.copyProperties (dictionaryAddFrom, activityDictionary);
        List<DictionaryAddFrom> acDic = activityDictionaryService.queryAllLevel (activityDictionary);
        return new ZR (1000, "查询成功", acDic);
    }


    /**
     * 添加
     *
     * @param dictionaryAddFrom
     * @return
     */
    @PostMapping("/saveLevel")
    @ApiOperation("增加")
    @Transactional//事务
    @Annotation2(tableName = "activity_dictionary", optionName = "增加")
    public ZR saveLevel(@Valid @RequestBody DictionaryAddFrom dictionaryAddFrom) {
        log.info ("开始新增*********************************");
        ActivityDictionary activityDictionary = new ActivityDictionary ();
        BeanUtil.copyProperties (dictionaryAddFrom, activityDictionary);
        activityDictionaryService.saveLevel (activityDictionary);
        return new ZR (1000, "增加成功", activityDictionary);
    }


    /**
     * 删除
     *
     * @param
     * @return
     */
    @DeleteMapping("/deleteLevel/{id}")
    @ApiOperation("删除")
    @Annotation2(tableName = "activity_dictionary", optionName = "删除", primaryKeyName = "id")
    @Transactional
    public ZR deleteLevel(@PathVariable Integer id) {
        log.info ("开始删除:{}", id);
        ActivityDictionary activityDictionary = new ActivityDictionary ();
        boolean state = activityDictionaryService.delete (id);
        if (state) {
            //删除缓存;就是Integer
            redisTemplate.delete (id + "");
            return new ZR (1000, "删除成功", id);
        }
        int i = 1 / 0;
        return new ZR (-1000, "删除失败", null);
    }


    /**
     * 修改
     *
     * @param activityDictionary
     * @return
     */
    @Annotation2(tableName = "activity_dictionary", optionName = "编辑")
    @PutMapping("/modifyUsers")
    @ApiOperation("编辑")
    @Transactional
    public ZR modifyEmps(@RequestBody ActivityDictionary activityDictionary) {
        log.info ("开始编辑:{}", activityDictionary);
        if (activityDictionaryService.updateById (activityDictionary)) {
            if (redisTemplate.hasKey (activityDictionary.getId () + "")) {
                //存在删除缓存
                redisTemplate.delete (activityDictionary.getId () + "");
            }
        }
        return new ZR (1000, "修改成功", activityDictionary);
    }


    /**
     *活动 随机类型下拉框查询：
     *
     * @return
     */
    @GetMapping("/queryTitle")
    @ApiOperation("随机类型查询")
    public ZR queryTitle() {
        return new ZR (1000, "查询成功", activityDictionaryService.queryTitle ());
    }


}
